//
//  TravelTableViewCell.m
//  TrapDemo
//
//  Created by zkingsoft on 16/2/26.
//  Copyright (c) 2016年 zkingsoft. All rights reserved.
//

#import "TravelTableViewCell.h"
#import "TravelModel.h"

@interface TravelTableViewCell ()

@property (nonatomic, strong) UIImageView   *headImageView;
@property (nonatomic, strong) UILabel       *headTitleLabel;
@property (nonatomic, strong) UILabel       *travelDaysLabel;

@property (nonatomic, strong) UILabel       *travelContentLabel;


@property (nonatomic, strong) UIView        *travelImagesOrVideoView;// 图片或视频
@property (nonatomic, strong) UIScrollView  *travelImageScrollView;
@property (nonatomic, strong) UIView        *travelVideoView;


@property (nonatomic, strong) UIView        *divideLine;// 分割线

@property (nonatomic, strong) UILabel       *goodLabel;
@property (nonatomic, strong) UILabel       *commentLabel;
@property (nonatomic, strong) UILabel       *travelTime;


//定义一个contentLabel文本高度的属性
@property (nonatomic, assign) CGFloat       travelContentLabelHeight;

@end

@implementation TravelTableViewCell

- (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier{
    self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
    if (self) {
        // 添加子控件
        [self setUpSubUI];
    }
    
    return self;
}


#pragma mark - 添加子控件
- (void)setUpSubUI{
    
    // 添加头像
    UIImageView *headImageView = [[UIImageView alloc] init];
    headImageView.layer.cornerRadius = 20;
    headImageView.contentMode = UIViewContentModeScaleAspectFill;// 设置图片的显示方式 带有scale的是保持比例不变的
    headImageView.layer.masksToBounds = YES;
    [self.contentView addSubview:headImageView];
    self.headImageView = headImageView;
    
    // travelDays
    UILabel *travelDaysLabel = [[UILabel alloc] init];
    travelDaysLabel.textAlignment = NSTextAlignmentCenter;
    [self.contentView addSubview:travelDaysLabel];
    self.travelDaysLabel = travelDaysLabel;
    
    // name
    UILabel *headTitleLabel = [[UILabel alloc] init];
    [self.contentView addSubview:headTitleLabel];
    self.headTitleLabel = headTitleLabel;
    
    
    // content
    UILabel *travelContentLabel = [[UILabel alloc] init];
    travelContentLabel.numberOfLines = 0;
    
    // 这一句一定要写 而且 大小和 自适应的时候使用的一定要一致 不然会出现 计算不精确的bug
    travelContentLabel.font = [UIFont systemFontOfSize:16];
    
    [self.contentView addSubview:travelContentLabel];
    self.travelContentLabel = travelContentLabel;
    
    // 图片或视频视图
    UIView *travelImagesOrVideoView = [[UIView alloc] init];
    travelImagesOrVideoView.backgroundColor = [UIColor redColor];
    [self.contentView addSubview:travelImagesOrVideoView];
    self.travelImagesOrVideoView = travelImagesOrVideoView;
    
    
    // =============
    // 创建weakSelf
    
    __weak __typeof(&*self)weakSelf = self;
    // __weak TravelTableViewCell *weakSelf = self;
    
    [self.headImageView makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(weakSelf.left).with.offset(10);
        make.top.equalTo(weakSelf.top).with.offset(10);
        make.width.and.height.equalTo(40);
    }];
    
    [self.travelDaysLabel makeConstraints:^(MASConstraintMaker *make) {
        make.right.equalTo(weakSelf.right).with.offset(-10);
        make.centerY.equalTo(weakSelf.headImageView.centerY);
        make.width.equalTo(120);
    }];
    
    [self.headTitleLabel makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(weakSelf.headImageView.right);
        make.right.equalTo(weakSelf.travelDaysLabel.left);
        make.centerY.equalTo(weakSelf.headImageView.centerY);
    }];
    
    [self.travelContentLabel makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(weakSelf.headImageView.bottom).with.offset(8);
        make.left.equalTo(weakSelf.left).with.offset(10);
        make.right.equalTo(weakSelf.right).with.offset(-10);
        
        // 文本的高度 等得到了模型的时候再传递
    }];
    
    [self.travelImagesOrVideoView makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(weakSelf.travelContentLabel.bottom).with.offset(8);
        make.left.equalTo(weakSelf.left).with.offset(10);
        make.right.equalTo(weakSelf.right).with.offset(-10);
        
        make.height.equalTo(60);// 这个也可以等有了模型的时候 再传递
    }];
}

#pragma mark - 传递数据模型
- (void)setTravelModel:(TravelModel *)travelModel{
    _travelModel = travelModel;
    
    self.headImageView.image = [UIImage imageNamed:travelModel.headImageString];
    self.travelDaysLabel.text = travelModel.travelDays;
    self.headTitleLabel.text = travelModel.headTitle;
    self.travelContentLabel.text = travelModel.travelContent;

    
    self.travelImagesOrVideoView.backgroundColor = [UIColor whiteColor];
    
    [self initScrollViewWithImageArray:travelModel.travelImageOrVideoArray];
}

#pragma mark - 图片 视频
- (void)initScrollViewWithImageArray:(NSMutableArray *)imageArray{
    if (!_travelImageScrollView) {
        _travelImageScrollView = [[UIScrollView alloc] init];
        _travelImageScrollView.showsHorizontalScrollIndicator = NO;
        _travelImageScrollView.showsVerticalScrollIndicator = NO;
        _travelImageScrollView.pagingEnabled = NO;
        [self.travelImagesOrVideoView addSubview:_travelImageScrollView];
        
        _travelImageScrollView.contentSize = CGSizeMake(120*imageArray.count, 60);
        
        __weak __typeof(&*self)weakSelf = self;
        [_travelImageScrollView makeConstraints:^(MASConstraintMaker *make) {
            make.left.equalTo(weakSelf.travelImagesOrVideoView.left);
            make.top.equalTo(weakSelf.travelImagesOrVideoView.top);
            make.right.equalTo(weakSelf.travelImagesOrVideoView.right);
            make.bottom.equalTo(weakSelf.travelImagesOrVideoView.bottom);
        }];
        
        [self layoutIfNeeded];
        
        for (int i = 0; i < imageArray.count; i++) {
            UIImageView *imageView = [[UIImageView alloc] initWithFrame:CGRectMake(120*i, 0, 110, 60)];
            imageView.userInteractionEnabled = YES;
            imageView.backgroundColor = [UIColor blueColor];
            imageView.image = [UIImage imageNamed:[NSString stringWithFormat:@"qsmy%d", i]];
            
            imageView.tag = 1000 + i;
            [_travelImageScrollView addSubview:imageView];
            
            UITapGestureRecognizer *tapGes = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(travelImageClicked:)];
            [imageView addGestureRecognizer:tapGes];
        }
    }
}

- (void)travelImageClicked:(UIGestureRecognizer *)ges{
    if ([_dele respondsToSelector:@selector(TravelImageClicked:)]) {
        UIImageView *imageView = (UIImageView *)ges.view;
        [_dele TravelImageClicked:imageView];
    }
}


- (void)initVideoViewWithVideoArray:(NSMutableArray *)videoArray{
    
}

#pragma mark - 根据数据模型得到cell的行高
- (CGFloat)rowHeightWithCellModel:(TravelModel *)travelModel{
    _travelModel = travelModel;
    
    // 设置文本的高度
    __weak __typeof(&*self)weakSelf = self;

    [self.travelContentLabel makeConstraints:^(MASConstraintMaker *make) {
        make.height.equalTo(weakSelf.travelContentLabelHeight);
    }];

    
    // 更新约束
    [self layoutIfNeeded];
    
    // 文本的最大高度
    CGFloat h = CGRectGetMaxY(self.travelContentLabel.frame);
    
    return h + 10 +60;// cell的最大高度
}

/*
 *  懒加载的方法返回contentLabel的高度  (只会调用一次)
 
    根据model中的文本内容计算出content的高度
 */
- (CGFloat)travelContentLabelHeight
{
    if(!_travelContentLabelHeight){
        CGFloat h=[self.travelModel.travelContent boundingRectWithSize:CGSizeMake(([UIScreen mainScreen].bounds.size.width)-2*10, MAXFLOAT) options:NSStringDrawingUsesLineFragmentOrigin attributes:@{NSFontAttributeName:[UIFont systemFontOfSize:16]} context:nil].size.height;
        
        _travelContentLabelHeight = h + 10;  //内容距离底部下划线10的距离
    }
    return _travelContentLabelHeight;
}


- (void)awakeFromNib {
    // Initialization code
}

- (void)setSelected:(BOOL)selected animated:(BOOL)animated {
    [super setSelected:selected animated:animated];

    // Configure the view for the selected state
}

@end
